<template>
  <el-card class="box-card" style="margin: 10px 0">
    <div slot="header" class="clearfix">
      <!-- 头部左侧 -->
      <el-tabs class="tabs" v-model="activeName">
        <el-tab-pane label="销售额" name="first"></el-tab-pane>
        <el-tab-pane label="访问量" name="second"></el-tab-pane>
      </el-tabs>
      <!-- 头部右侧 -->
      <div class="right">
        <span @click="setDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <!-- 日期选择器 -->
        <el-date-picker
          v-model="data"
          type="daterange"
          range-separator="-"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 250px"
        >
        </el-date-picker>
      </div>
    </div>
    <div class="content">
      <el-row :gutter="10">
        <el-col :span="18">
          <!-- echarts容器 -->
          <div
            ref="chart"
            style="width: 800px; height: 300px; margin-top: -20px"
          ></div>
        </el-col>
        <el-col :span="6">
          <el-row :gutter="10">
            <el-col :span="24" class="title"
              ><span>门店{{ title }}排名</span></el-col
            >
          </el-row>
          <el-row :gutter="10">
            <el-col :span="2">
              <span>1</span>
            </el-col>
            <el-col :span="16">
              <span>京东</span>
            </el-col>
            <el-col :span="6">
              <span>999999</span>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="2">
              <span>2</span>
            </el-col>
            <el-col :span="16">
              <span>淘宝</span>
            </el-col>
            <el-col :span="6">
              <span>888888</span>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="2">
              <span>3</span>
            </el-col>
            <el-col :span="16">
              <span>拼多多</span>
            </el-col>
            <el-col :span="6">
              <span>777777</span>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="2">
              <span>4</span>
            </el-col>
            <el-col :span="16">
              <span>天猫</span>
            </el-col>
            <el-col :span="6">
              <span>666666</span>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="2">
              <span>5</span>
            </el-col>
            <el-col :span="16">
              <span>京喜</span>
            </el-col>
            <el-col :span="6">
              <span>665544</span>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="2">
              <span>6</span>
            </el-col>
            <el-col :span="16">
              <span>毒</span>
            </el-col>
            <el-col :span="6">
              <span>603595</span>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="2">
              <span>7</span>
            </el-col>
            <el-col :span="16">
              <span>尚品会</span>
            </el-col>
            <el-col :span="6">
              <span>600000</span>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
import * as echarts from "echarts";
import {mapGetters} from 'vuex'
// 引入操作时间的插件
import dayjs from "dayjs";
export default {
  name: "Middle",
  data() {
    return {
      // 保存echarts实例
      myEchart: null,
      // 日期选择器保存起始日期与结束日期
      data: [],
      // tabs激活
      activeName: "first",
    };
  },
  mounted() {
    this.myEchart = echarts.init(this.$refs.chart);
    this.myEchart.setOption({
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: [],
        },
      ],
    });
  },
  methods: {
    setDay() {
      const day = dayjs().startOf("day").format("YYYY-MM-DD");
      this.data = [day, day];
    },
    setWeek() {
      const sWeek = dayjs().day(1).format("YYYY-MM-DD");
      const eWeek = dayjs().day(7).format("YYYY-MM-DD");
      this.data = [sWeek, eWeek];
    },
    setMonth() {
      const sMonth = dayjs().startOf("month").format("YYYY-MM-DD");
      const eMonth = dayjs().endOf("month").format("YYYY-MM-DD");
      this.data = [sMonth, eMonth];
    },
    setYear() {
      const sYear = dayjs().startOf("year").format("YYYY-MM-DD");
      const eYear = dayjs().endOf("year").format("YYYY-MM-DD");
      this.data = [sYear, eYear];
    },
  },
  computed: {
    title() {
      return this.activeName === "first" ? "销售额" : "访问量";
    },
    ...mapGetters(['chartMockData'])
  },
  watch: {
    // 监听title的变化，修改echarts的title
    title() {
      // 修改chart数据
      this.myEchart.setOption({
        title: {
          text: this.title,
        },
        xAxis:{
          data:this.title === "销售额" ? this.chartMockData.orderFullYearAxis : this.chartMockData.userFullYearAxis
        },
        series:[{
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data:this.title === "销售额" ? this.chartMockData.orderFullYear : this.chartMockData.userFullYear
        }]
      });
    },
    // 当数据返回时，给表格传递数据进行展示
    chartMockData(){
      this.myEchart.setOption({
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: this.chartMockData.userFullYearAxis,
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: this.chartMockData.userFullYear,
        },
      ],
    });
    }
  },
};
</script>

<style>
.el-card__header {
  border-bottom: none;
}
</style>
<style lang="less" scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.right {
  position: absolute;
  right: 0;
  // top: 10px;
  span {
    font-size: 14px;
    margin: 0 10px;
  }
}
.tabs {
  width: 100%;
}
.content {
  .title {
    font-weight: bold;
    font-size: 18px;
    margin-top: -20px;
  }
  span {
    display: block;
    margin: 5px 0;
  }
}
</style>